import uuid

import vtk

from CloudEngine.IO.IVTPReader import IVTPReader
from CloudEngine.core.VTActor import VTActor
from service.DataCenter import db
from service.utils.download_file import download_file


def read_vtp(service, params):
    path = params.get("path")
    entry_file = str(uuid.uuid4()) + ".vtp"
    local_path = "./tmp/" + entry_file
    if path.find("http") != -1:
        download_file(path, local_path)
    else:
        db.S3.download_file(path, local_path)
    vtp_reader = IVTPReader()
    vtp_reader.read(local_path)
    ac = VTActor(vtp_reader.get_poly_data(), {
        "parse_attributes": True
    })
    ac.set_render_type("SURFACE_WITH_EDGE")
    service.model_add(ac)
